package com.ando.aes;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Ando
 * @version 1.0
 * @date 2022/6/3
 */
public class UI {

    /**
     * @author EdwinD
     * @create 2020.08.18 上午 07:28
     * @desc
     **/

    public static void main(String[] args) {

        final String[] srcFilePath = new String[2];
//         final String destFilePath = "";


        Frame frame = new Frame("AES加密解密");
        //创建两个FileDialog对象
        FileDialog f1 = new FileDialog(frame, "选择加/解密要打开的文件", FileDialog.LOAD);
        FileDialog f2 = new FileDialog(frame, "选择解/加密要保存的路径", FileDialog.SAVE);

        Button b1 = new Button("请选择您要加/解密的文件");
        TextField key = new TextField(16);//字符数
        Button b2 = new Button("请选择您加/解密后保存的文件");

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f1.setVisible(true);//代码阻塞在此

                //获取选择的路径及文件
                String directory1 = f1.getDirectory();
                String file1 = f1.getFile();
                srcFilePath[0] = directory1 + file1;
//                System.out.println(directory1 + "##" + file1);
                System.out.println(srcFilePath[0]);
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f2.setVisible(true);//代码阻塞在此

                //获取选择的路径及文件
                String directory2 = f2.getDirectory();
                String file2 = f2.getFile();
                srcFilePath[1] = directory2 + file2;
//                System.out.println(directory2 + "^^" + file2);
//                System.out.println(srcFilePath[0]);
                System.out.println(srcFilePath[1]);
            }
        });

//        一个按钮
        Button button1 = new Button("ECB加密");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String key1 = key.getText();
                System.out.println(srcFilePath[0] + srcFilePath[1] + key1);
                EncryptFile.encryptFileECB(srcFilePath[0], srcFilePath[1], key1);

            }
        });
//        button1.addActionListener(new AESListener1(srcFilePath[0], key, srcFilePath[1]));
        Button button2 = new Button("ECB解密");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String key1 = key.getText();
                DecryptFile.decryptFileECB(srcFilePath[0], srcFilePath[1], key1);

            }
        });
        Button button3 = new Button("CBC加密");
        button3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String key1 = key.getText();
                try {
                    EncryptFile.encryptFileCBC(srcFilePath[0], srcFilePath[1], key1);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

            }
        });
        Button button4 = new Button("CBC解密");
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String key1 = key.getText();
                try {
                    DecryptFile.decryptFileCBC(srcFilePath[0], srcFilePath[1], key1);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

            }
        });
//        一个标签
        Label label = new Label("请输入您的密钥(16位，不含中文):");
//        布局
        frame.setLayout(new FlowLayout());
        frame.add(b1, BorderLayout.NORTH);
        frame.add(b2, BorderLayout.NORTH);
        frame.add(label, BorderLayout.CENTER);
        frame.add(key);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);


        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
//        frame.setBounds(377, 377, 300, 200);

    }

//    //监听器类
//    static class AESListener1 implements ActionListener {
//         final String srcFilePath;
//         final TextField key;
//         final String destFilePath;
//
//        //    获取三个变量
//        public AESListener1(String srcFilePath, TextField key, String destFilePath) {
//            this.srcFilePath = srcFilePath;
//            this.key = key;
//            this.destFilePath = destFilePath;
//        }
//
//        @Override
//        public void actionPerformed(ActionEvent e) {
//            String key1 = key.getText();
//            System.out.println(srcFilePath + "+" + destFilePath + "+" + key1);
//            EncryptFile.encryptFile(srcFilePath,destFilePath,key1);
//        }


}
//监听器类
//    static class AESListener2 implements ActionListener {
//        private final String srcFilePath;
//        private final TextField key;
//        private final String destFilePath;
//
//        //    获取三个变量
//        public AESListener2(String srcFilePath, TextField key, String destFilePath) {
//            this.srcFilePath = srcFilePath;
//            this.key = key;
//            this.destFilePath = destFilePath;
//        }
//
//        @Override
//        public void actionPerformed(ActionEvent e) {
//            String key1 = key.toString();
//
//////        1.获得加数,被加数
////            int n1 = Integer.parseInt(num1.getText());
////            int n2 = Integer.parseInt(num2.getText());
//////        2.将两个值加法运算后,放到第三个框中,
////            num3.setText("" + (n1 + n2));
//////        3.清理前两个框中的数字
////            num1.setText("");
////            num2.setText("");
//            DecryptFile.decryptFile(srcFilePath, destFilePath, key1);
//        }
//    }}
